//
//  LCScrollView.m
//  LCScrollView
//
//  Created by 王立诚 on 16/4/12.
//  Copyright © 2016年 王立诚. All rights reserved.
//

#import "LCScrollView.h"
@interface LCScrollView()
@property(nonatomic,strong)NSMutableArray *buttons;
@property(nonatomic,strong)UIButton *selectedButton;
@end

@implementation LCScrollView
-(NSMutableArray *)buttons
{
    if (_buttons == nil)
    {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}
-(instancetype)initWithFrame:(CGRect)frame itemSize:(CGSize)itemSize bigItemSize:(CGSize)bigItemSize images:(NSArray *)images itemSpacing:(CGFloat)itemSpacing
{
    if (self = [super initWithFrame:frame]) {
        
        self.itemSpacing = itemSpacing;
        self.itemSize = itemSize;
        self.bigItemSize = bigItemSize;
        self.images = images;
        self.contentSize = CGSizeMake(itemSize.width * 3 * images.count, frame.size.height);
        self.delegate = self;
        for(int i = 0; i < images.count * 3; i++)
        {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(i * (itemSize.width + itemSpacing), 0, itemSize.width, itemSize.height);
            button.tag = i;
            if(i < images.count)
            {
                [button setImage:images[i] forState:UIControlStateNormal];
            }else{
                [button setImage:images[i % images.count] forState:UIControlStateNormal];
            }
            [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            [self.buttons addObject:button];
            [self addSubview:button];
        }
        
    }
    return self;
}

-(void)setSSelectedItem:(NSInteger)seletedTag
{
    self.selectedButton = self.buttons[seletedTag];
    [self setContentOffset:CGPointMake(self.selectedButton.center.x - self.bounds.size.width * 0.5, 0) animated:YES];
}

-(void)click:(UIButton *)button
{
    [self setSSelectedItem:button.tag];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    if (self.contentOffset.x < 0)
    {
        CGPoint contentOffset = self.contentOffset;
        contentOffset.x += ((self.images.count)) * (self.itemSize.width) + (self.itemSpacing + (self.itemSize.width)) * 0.5;
        self.contentOffset = contentOffset;
        
        
    }else if (self.contentOffset.x > ((self.images.count)) * 3 * (self.itemSize.width) - self.bounds.size.width)
    {
        CGPoint contentOffset = self.contentOffset;
        contentOffset.x -= ((self.images.count)) * (self.itemSize.width) + (self.itemSpacing + (self.itemSize.width)) * 0.5;
        self.contentOffset = contentOffset;
    }
    [self adjustSize];
}

-(void)adjustSize
{
    for(UIButton *button in self.buttons)
    {
        CGFloat distance = CGRectGetMidX(button.frame) - CGRectGetMidX(self.bounds);
        if(distance > self.itemSize.width * 0.5 || distance < self.itemSize.width * 0.5 * -1)
        {
            CGRect bounds = button.bounds;
            bounds.size = self.itemSize;
            button.bounds = bounds;
        }else{
            CGFloat k = 2 * (self.itemSize.width - self.bigItemSize.width)/(self.itemSize.width);
            CGFloat b = self.bigItemSize.width;
            
            if (distance > 0)
            {
                CGRect bounds = button.bounds;
                bounds.size = CGSizeMake(k * distance + b, k * distance + b);
                button.bounds = bounds;
            }else{
                CGRect bounds = button.bounds;
                bounds.size = CGSizeMake(-k * distance + b, -k * distance + b);
                button.bounds = bounds;
            }
        }
    }
}

#pragma mark : - SCROLLVIEWDELEGATE
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat width = self.bounds.size.width ;
    NSInteger selectedTag = (self.contentOffset.x + (width - self.itemSize.width) * 0.5) / (self.itemSize.width + self.itemSpacing);
    [self setSSelectedItem:selectedTag];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    CGFloat width = self.bounds.size.width;
    NSInteger selectedTag = (self.contentOffset.x + (width - self.itemSize.width) * 0.5) / (self.itemSize.width + self.itemSpacing);
    [self setSSelectedItem:selectedTag];
}


@end
